import urllib
import urllib2
import smtplib, string, sys
from sgmllib import SGMLParser

# settings
server = 's2.travian.ru'
#login = 'vii'
#password = '001222'
login = 'megamen'
password = 'gavr@trav'
resolution='1280:1024'
FROMADDR = 'gurianov@geo-s.ru'        # Author is Donn Lee.
SMTPSERVER = 'www.loc'    # Put your own email server here.
toaddrs = ['79277078010@sms.mgsm.ru','alexey@gurianov.com']
subj = 'Travian!'

# request params
useragent = 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9) Gecko/2008061015 Firefox/3.0'
headers = {'Accept' : 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
           'Accept-Language' : 'en-us,en;q=0.5',
           'Accept-Charset' : 'ISO-8859-1,utf-8;q=0.7,*;q=0.7',
           'Keep-Alive' : '300',
           'Connection' : 'keep-alive',
           'Refferer' : 'http://s3.travian.ru/login.php',
           'User-Agent' : useragent,
           'Accept-Encoding' : 'gzip,deflate',
           }
values = {}

# Login travian parser
class LoginParser(SGMLParser):
    def reset(self):
        SGMLParser.reset(self)
        self.form = {}
        self.logintag=''
        self.passtag=''

    def start_input(self, attrs):
        name = [v for k, v in attrs if k=='name'][0]
        value = [v for k, v in attrs if k=='value'][0]
        type = [v for k, v in attrs if k=='type'][0]
        if name:
            self.form[name]=value
        if type=='text': self.logintag=name
        elif type=='password': self.passtag=name

class Dorf1Parser(SGMLParser):
    def reset(self):
        SGMLParser.reset(self)
        self.resid =''
        self.resvalues = {}
        self.resdelta = {}

    def start_td(self, attrs):
        id = [v for k, v in attrs if k=='id']
        title = [v for k, v in attrs if k=='title']
        if not id: id=['bad']
        if id[0] in ('l1','l2','l3','l4'):
            self.resid=id[0]
            self.resdelta[id[0]]=title[0]
    
    def end_td(self):
        self.resid=''
        
    def handle_data(self, text):
        if self.resid in ('l1','l2','l3','l4'):
            self.resvalues[self.resid]=text

def EmailOut(toaddrs, subj, msg):
   # SIMPLE FUNCTION THAT SENDS EMAIL.
   # toaddrs MUST be a python list of email addresses.

   # Convert list to string.
   s_toaddrs = string.join(toaddrs, ",")
   # Convert msg to smtp format.
   msg = """\
To: %s
From: %s
Subject: %s

%s
""" % (s_toaddrs, FROMADDR, subj, msg)
   try:
      server = smtplib.SMTP(SMTPSERVER)
      # If your mail server requires a username/login, you'll need the following line.
      #server.login('gurianov@geo-s.ru', 'gavr@geos')
      server.sendmail(FROMADDR, toaddrs, msg)
      server.quit()
   except:
      print "ERROR SENDING EMAIL! SMTP ERROR."
      sys.exit()

def getinfo():
    # Request a page
    url = 'http://'+server+'/dorf1.php'
    req = urllib2.Request(url)
    response = urllib2.urlopen(req)
    the_page = response.read()
    #print the_page
    #init parser
    parser = LoginParser()
    parser.feed(the_page)
    parser.close()
    
    #filling form
    values=parser.form
    logintag=parser.logintag
    passtag=parser.passtag
    
    values['w']=resolution
    values[logintag]=login
    values[passtag]=password
    values['s1.x']='0'
    values['s1.y']='0'
    print values
    #sending form and getting dorf1
    data = urllib.urlencode(values)
    req = urllib2.Request(url, data)
    response = urllib2.urlopen(req)
    the_page = response.read()
    
    parser = Dorf1Parser()
    parser.feed(the_page)
    parser.close()
    text=(parser.resvalues, parser.resdelta)
    (text, response.info()['Set-Cookie'])
    #EmailOut(toaddrs, subj, text)
    
getinfo()
